package graphs

import (
	"power/core/common/constant"
	"power/core/dao/graphdao"
	"power/core/dao/table"
	"power/core/localwrap"
	"power/core/service/graph"

	"github.com/RocksonZeta/wrap/utils/cutil"
)

var logProjectUsers = localwrap.GetLogger().Fork("power/core/service/graphs", "ProjectUsers")

type ProjectUsers struct{}

func (s ProjectUsers) Query(param graphdao.ProjectUsersQueryParam) ([]ProjectUser, int64) {
	logProjectUsers.Debug().Func("Query").Interface("param", param).Send()
	d := localwrap.GetRedisedMysql()
	defer d.Close()

	pus, total := graphdao.ProjectUsers{RedisedMysql: d}.Query(param)
	if len(pus) == 0 {
		return nil, 0
	}
	projects := graphdao.Projects{RedisedMysql: d}.ListAsMap(cutil.ColInt(pus, "ProjectId"))
	r := make([]ProjectUser, len(pus))
	for i, v := range pus {
		r[i].Project = projects[v.ProjectId]
		r[i].ProjectUser = v
	}
	return r, total
}

type ProjectUser struct {
	table.Project
	ProjectUser table.ProjectUser
}

func (s ProjectUsers) Get(projectId, uid int) ProjectUser {
	logProjectUsers.Debug().Func("Get").Int("projectId", projectId).Int("uid", uid).Send()
	d := localwrap.GetRedisedMysql()
	defer d.Close()
	var r ProjectUser
	r.ProjectUser = graphdao.ProjectUsers{RedisedMysql: d}.GetByUidProjectId(uid, projectId)
	r.Project = graphdao.Projects{RedisedMysql: d}.Get(projectId)
	return r
}

type ProjectUsersAddParam struct {
	graphdao.ProjectsAddParam
	User graphdao.ProjectUsersAddParam
}

func (s ProjectUsers) Add(param ProjectUsersAddParam) table.ProjectUser {
	logProjectUsers.Debug().Func("Add").Interface("param", param).Send()
	d := localwrap.GetRedisedMysql()
	defer d.Close()
	props := graph.NewProps()
	if param.MapCountry == "cn" {
		props["map"] = "cn"
		props["x"] = 39.9
		props["y"] = 116.38
		props["zoom"] = 7
	}
	g := graphdao.Graphs{RedisedMysql: d}.Add(graphdao.GraphsAddParam{Name: param.Name, Creator: param.Uid, Props: props.Marshal()})
	param.ProjectsAddParam.GraphId = g.Id
	p := graphdao.Projects{RedisedMysql: d}.Add(param.ProjectsAddParam)
	param.User.Uid = param.Uid
	param.User.ProjectId = p.Id
	param.User.Role = constant.ProjectOwner
	return graphdao.ProjectUsers{RedisedMysql: d}.Add(param.User)
}

func (s ProjectUsers) Update(param graphdao.ProjectUsersUpdateParam) {
	logProjectUsers.Debug().Func("Update").Interface("param", param).Send()
	d := localwrap.GetRedisedMysql()
	defer d.Close()
	graphdao.ProjectUsers{RedisedMysql: d}.Update(param)
}

func (s ProjectUsers) Delete(id int) {
	logProjectUsers.Debug().Func("Delete").Int("id", id).Send()
	d := localwrap.GetRedisedMysql()
	defer d.Close()
	graphdao.ProjectUsers{RedisedMysql: d}.Delete(id)
}

//GetByUidProjectId
func (s ProjectUsers) GetByUidProjectId(uid int, projectId int) table.ProjectUser {
	logProjectUsers.Debug().Func("GetByUidProjectId").Int("uid", uid).Int("projectId", projectId).Send()
	d := localwrap.GetRedisedMysql()
	defer d.Close()
	return graphdao.ProjectUsers{RedisedMysql: d}.GetByUidProjectId(uid, projectId)
}

//ListByProjectId
func (s ProjectUsers) ListByProjectId(projectId int) []table.ProjectUser {
	logProjectUsers.Debug().Func("ListByProjectId").Int("projectId", projectId).Send()
	d := localwrap.GetRedisedMysql()
	defer d.Close()
	return graphdao.ProjectUsers{RedisedMysql: d}.ListByProjectId(projectId)
}

//ListByUid
func (s ProjectUsers) ListByUid(uid int) []table.ProjectUser {
	logProjectUsers.Debug().Func("ListByUid").Int("uid", uid).Send()
	d := localwrap.GetRedisedMysql()
	defer d.Close()
	return graphdao.ProjectUsers{RedisedMysql: d}.ListByUid(uid)
}
